<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="#课程目标  掌握case语句的基本语法结构 掌握函数的定义及调用 掌握常用的正则表达式元字符含义">
<meta property="og:type" content="article">
<meta property="og:title" content="06_SHELL编程之CASE语句+函数+正则">
<meta property="og:url" content="http://example.com/2021/01/06/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/06_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8BCASE%E8%AF%AD%E5%8F%A5+%E5%87%BD%E6%95%B0+%E6%AD%A3%E5%88%99/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="#课程目标  掌握case语句的基本语法结构 掌握函数的定义及调用 掌握常用的正则表达式元字符含义">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-06T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-17T07:38:10.552Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/01/06/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/06_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8BCASE%E8%AF%AD%E5%8F%A5+%E5%87%BD%E6%95%B0+%E6%AD%A3%E5%88%99/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>06_SHELL编程之CASE语句+函数+正则 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/06/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/06_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8BCASE%E8%AF%AD%E5%8F%A5+%E5%87%BD%E6%95%B0+%E6%AD%A3%E5%88%99/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          06_SHELL编程之CASE语句+函数+正则
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-06 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-06T20:46:25+08:00">2021-01-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-17 15:38:10" itemprop="dateModified" datetime="2021-02-17T15:38:10+08:00">2021-02-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>14k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>13 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>#课程目标</p>
<ul>
<li>掌握case语句的基本语法结构</li>
<li>掌握函数的定义及调用</li>
<li>掌握常用的正则表达式元字符含义</li>
</ul>
<a id="more"></a>

<h1 id="一、case语句"><a href="#一、case语句" class="headerlink" title="一、case语句"></a>一、case语句</h1><p><strong>关键词：确认过眼神，你是对的人</strong>:couple_with_heart:</p>
<ol>
<li>case语句为多重匹配语句</li>
<li>如果匹配成功，执行相匹配的命令</li>
</ol>
<h2 id="1-语法结构"><a href="#1-语法结构" class="headerlink" title="1. 语法结构"></a>1. 语法结构</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">说明：pattern表示需要匹配的模式</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">case var <span class="keyword">in</span>             定义变量;var代表是变量名</span><br><span class="line">pattern <span class="number">1</span>)              模式<span class="number">1</span>;用 | 分割多个模式，相当于or</span><br><span class="line">    command1            需要执行的语句</span><br><span class="line">    ;;                  两个分号代表命令结束</span><br><span class="line">pattern <span class="number">2</span>)</span><br><span class="line">    command2</span><br><span class="line">    ;;</span><br><span class="line">pattern <span class="number">3</span>)</span><br><span class="line">    command3</span><br><span class="line">    ;;</span><br><span class="line">		  *)              default，不满足以上模式，默认执行*)下面的语句</span><br><span class="line">    command4</span><br><span class="line">    ;;</span><br><span class="line">esac							esac表示case语句结束</span><br></pre></td></tr></table></figure>
<h2 id="2-应用案例"><a href="#2-应用案例" class="headerlink" title="2. 应用案例"></a>2. 应用案例</h2><h3 id="㈠-脚本传不同值做不同事"><a href="#㈠-脚本传不同值做不同事" class="headerlink" title="㈠ 脚本传不同值做不同事"></a>㈠ 脚本传不同值做不同事</h3><p><strong>具体需求：</strong>当给程序传入start、stop、restart三个不同参数时分别执行相应命令</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">case <span class="variable">$1</span> <span class="keyword">in</span></span><br><span class="line">        <span class="built_in">start</span>|S)</span><br><span class="line">        service apache <span class="built_in">start</span> &amp;&gt;/dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;apache 启动成功&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">        stop|T)</span><br><span class="line">        service apache stop &amp;&gt;/dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;apache 停止成功&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">        restart|<span class="built_in">R</span>)</span><br><span class="line">        service apache restart &amp;&gt;/dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;apache 重启完毕&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">        *)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;请输入要做的事情...&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">esac</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-根据用户需求选择做事"><a href="#㈡-根据用户需求选择做事" class="headerlink" title="㈡ 根据用户需求选择做事"></a>㈡ 根据用户需求选择做事</h3><p><strong>具体需求：</strong></p>
<p>脚本提示让用户输入需要管理的服务名，然后提示用户需要对服务做什么操作，如启动，关闭等操作</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你要管理的服务名称(vsftpd):&quot;</span> service</span><br><span class="line">case <span class="variable">$service</span> <span class="keyword">in</span></span><br><span class="line">        vsftpd|ftp)</span><br><span class="line">        read <span class="literal">-p</span> <span class="string">&quot;请选择你需要做的事情(restart|stop):&quot;</span> action</span><br><span class="line">        case <span class="variable">$action</span> <span class="keyword">in</span></span><br><span class="line">                stop|S)</span><br><span class="line">                service vsftpd stop &amp;&gt;/dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;该<span class="variable">$serivce</span>服务已经停止成功&quot;</span></span><br><span class="line">                ;;</span><br><span class="line">                <span class="built_in">start</span>)</span><br><span class="line">                service vsftpd <span class="built_in">start</span> &amp;&gt;/dev/null &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;该<span class="variable">$serivce</span>服务已经成功启动&quot;</span></span><br><span class="line">                ;;</span><br><span class="line">        esac</span><br><span class="line">        ;;</span><br><span class="line">        httpd|apache)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;apache hello world&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">        *)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;请输入你要管理的服务名称(vsftpd)&quot;</span></span><br><span class="line">        ;;</span><br><span class="line">esac</span><br><span class="line">	</span><br></pre></td></tr></table></figure>
<p>###㈢ 菜单提示让用户选择需要做的事</p>
<p><strong>具体需求：</strong></p>
<p>模拟一个多任务维护界面;当执行程序时先显示总菜单，然后进行选择后做相应维护监控操作</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">**********请选择*********</span><br><span class="line"><span class="built_in">h</span>	显示命令帮助</span><br><span class="line">f	显示磁盘分区</span><br><span class="line">d	显示磁盘挂载</span><br><span class="line">m	查看内存使用</span><br><span class="line">u	查看系统负载</span><br><span class="line">q	退出程序</span><br><span class="line">*************************</span><br></pre></td></tr></table></figure>
<p><strong>思路：</strong></p>
<ol>
<li>菜单打印出来</li>
<li>交互式让用户输入操作编号，然后做出相应处理</li>
</ol>
<p><strong>落地实现：</strong></p>
<ol>
<li>菜单打印(分解动作)</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line">	<span class="built_in">h</span>	显示命令帮助</span><br><span class="line">	f	显示磁盘分区</span><br><span class="line">	d	显示磁盘挂载</span><br><span class="line">	m	查看内存使用</span><br><span class="line">	u	查看系统负载</span><br><span class="line">	q	退出程序</span><br><span class="line">	EOF</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>最终实现</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#打印菜单</span></span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line">	<span class="built_in">h</span>	显示命令帮助</span><br><span class="line">	f	显示磁盘分区</span><br><span class="line">	d	显示磁盘挂载</span><br><span class="line">	m	查看内存使用</span><br><span class="line">	u	查看系统负载</span><br><span class="line">	q	退出程序</span><br><span class="line">	EOF</span><br><span class="line"></span><br><span class="line"><span class="comment">#让用户输入需要的操作</span></span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入需要操作的选项[f|d]:&quot;</span> var1</span><br><span class="line">case <span class="variable">$var1</span> <span class="keyword">in</span></span><br><span class="line">	<span class="built_in">h</span>)</span><br><span class="line">	<span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line">        <span class="built_in">h</span>       显示命令帮助</span><br><span class="line">        f       显示磁盘分区</span><br><span class="line">        d       显示磁盘挂载</span><br><span class="line">        m       查看内存使用</span><br><span class="line">        u       查看系统负载</span><br><span class="line">        q       退出程序</span><br><span class="line">	EOF</span><br><span class="line">	;;</span><br><span class="line">	f)</span><br><span class="line">	fdisk <span class="literal">-l</span></span><br><span class="line">	;;</span><br><span class="line">	d)</span><br><span class="line">	df <span class="literal">-h</span></span><br><span class="line">	;;</span><br><span class="line">	m)</span><br><span class="line">	free <span class="literal">-m</span></span><br><span class="line">	;;</span><br><span class="line">	u)</span><br><span class="line">	uptime</span><br><span class="line">	;;</span><br><span class="line">	q)</span><br><span class="line">	<span class="keyword">exit</span></span><br><span class="line">	;;</span><br><span class="line">esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#打印菜单</span></span><br><span class="line">menu()&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-END</span></span><br><span class="line">	<span class="built_in">h</span>	显示命令帮助</span><br><span class="line">	f	显示磁盘分区</span><br><span class="line">	d	显示磁盘挂载</span><br><span class="line">	m	查看内存使用</span><br><span class="line">	u	查看系统负载</span><br><span class="line">	q	退出程序</span><br><span class="line">	<span class="keyword">END</span></span><br><span class="line">&#125;</span><br><span class="line">menu</span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你的操作[h for help]:&quot;</span> var1</span><br><span class="line">case <span class="variable">$var1</span> <span class="keyword">in</span></span><br><span class="line">	<span class="built_in">h</span>)</span><br><span class="line">	menu</span><br><span class="line">	;;</span><br><span class="line">	f)</span><br><span class="line">	read <span class="literal">-p</span> <span class="string">&quot;请输入你要查看的设备名字[/dev/sdb]:&quot;</span> var2</span><br><span class="line">	case <span class="variable">$var2</span> <span class="keyword">in</span></span><br><span class="line">		/dev/sda)</span><br><span class="line">		fdisk <span class="literal">-l</span> /dev/sda</span><br><span class="line">		;;</span><br><span class="line">		/dev/sdb)</span><br><span class="line">		fdisk <span class="literal">-l</span> /dev/sdb</span><br><span class="line">		;;</span><br><span class="line">	esac</span><br><span class="line">	;;</span><br><span class="line">	d)</span><br><span class="line">	lsblk</span><br><span class="line">	;;</span><br><span class="line">	m)</span><br><span class="line">	free <span class="literal">-m</span></span><br><span class="line">	;;</span><br><span class="line">	u)</span><br><span class="line">	uptime</span><br><span class="line">	;;</span><br><span class="line">	q)</span><br><span class="line">	<span class="keyword">exit</span></span><br><span class="line">	;;</span><br><span class="line">esac</span><br><span class="line">done</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>课堂练习：</strong></p>
<ol>
<li>输入一个等级（A-E），查看每个等级的成绩；如：输入A，则显示“90分~100分”，依次类推</li>
<li>判断用户输入的字符串，如果是”hello”,则显示”world”；如果是”world”,则显示”hello”,否则提示”请输入hello或者world，谢谢！”</li>
</ol>
<h1 id="二、-函数"><a href="#二、-函数" class="headerlink" title="二、==函数=="></a>二、==函数==</h1><h2 id="1-什么是函数？"><a href="#1-什么是函数？" class="headerlink" title="1. 什么是函数？"></a>1. 什么是函数？</h2><ul>
<li>shell中允许将<strong>一组命令集合</strong>或<strong>语句</strong>形成一段<strong>可用代码</strong>，这些代码块称为shell函数</li>
<li>给这段代码起个名字称为函数名，后续可以直接调用该段代码的功能</li>
</ul>
<h2 id="2-如何定义函数？"><a href="#2-如何定义函数？" class="headerlink" title="2. 如何定义函数？"></a>2. 如何定义函数？</h2><p><strong>方法1：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">函数名()</span><br><span class="line">&#123;</span><br><span class="line">  函数体（一堆命令的集合，来实现某个功能）   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>方法2：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> 函数名<span class="params">()</span></span></span><br><span class="line">&#123;</span><br><span class="line">   函数体（一堆命令的集合，来实现某个功能）</span><br><span class="line">   <span class="built_in">echo</span> hello</span><br><span class="line">   <span class="built_in">echo</span> world</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>函数中==return==说明:</strong></p>
<ol>
<li>return可以==结束一个函数==。类似于循环控制语句break(结束当前循环，执行循环体后面的代码)。</li>
<li>return默认返回函数中最后一个命令状态值，也可以给定参数值，范围是0-256之间。</li>
<li>如果没有return命令，函数将返回最后一个指令的退出状态值。</li>
</ol>
<p>##3. 函数如何调用？</p>
<h3 id="㈠-当前命令行调用"><a href="#㈠-当前命令行调用" class="headerlink" title="㈠ 当前命令行调用"></a>㈠ 当前命令行调用</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># cat fun1.sh </span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">hello()&#123;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;hello lilei <span class="variable">$1</span>&quot;</span></span><br><span class="line">hostname</span><br><span class="line">&#125;</span><br><span class="line">menu()&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line"><span class="number">1</span>. mysql</span><br><span class="line"><span class="number">2</span>. web</span><br><span class="line"><span class="number">3</span>. app</span><br><span class="line"><span class="number">4</span>. <span class="keyword">exit</span></span><br><span class="line">EOF</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># source fun1.sh </span></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># . fun1.sh </span></span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># hello 888</span></span><br><span class="line">hello lilei <span class="number">888</span></span><br><span class="line">MissHou.itcast.cc</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># menu</span></span><br><span class="line"><span class="number">1</span>. mysql</span><br><span class="line"><span class="number">2</span>. web</span><br><span class="line"><span class="number">3</span>. app</span><br><span class="line"><span class="number">4</span>. <span class="keyword">exit</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-定义到用户的环境变量中"><a href="#㈡-定义到用户的环境变量中" class="headerlink" title="㈡ 定义到用户的环境变量中"></a>㈡ 定义到用户的环境变量中</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell05</span>]<span class="comment"># vim ~/.bashrc </span></span><br><span class="line">文件中增加如下内容：</span><br><span class="line">hello()&#123;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;hello lilei <span class="variable">$1</span>&quot;</span></span><br><span class="line">hostname</span><br><span class="line">&#125;</span><br><span class="line">menu()&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line"><span class="number">1</span>. mysql</span><br><span class="line"><span class="number">2</span>. web</span><br><span class="line"><span class="number">3</span>. app</span><br><span class="line"><span class="number">4</span>. <span class="keyword">exit</span></span><br><span class="line">EOF</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">注意：</span><br><span class="line">当用户打开bash的时候会读取该文件</span><br></pre></td></tr></table></figure>
<h3 id="㈢-脚本中调用"><a href="#㈢-脚本中调用" class="headerlink" title="㈢ 脚本中调用"></a>㈢ 脚本中调用</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#打印菜单</span></span><br><span class="line">source ./fun1.sh</span><br><span class="line">menu()&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-END</span></span><br><span class="line">	<span class="built_in">h</span>	显示命令帮助</span><br><span class="line">	f	显示磁盘分区</span><br><span class="line">	d	显示磁盘挂载</span><br><span class="line">	m	查看内存使用</span><br><span class="line">	u	查看系统负载</span><br><span class="line">	q	退出程序</span><br><span class="line">	<span class="keyword">END</span></span><br><span class="line">&#125;</span><br><span class="line">menu		//调用函数</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>##4. 应用案例</p>
<p><strong>具体需求：</strong></p>
<ol>
<li>写一个脚本==收集用户输入==的基本信息(姓名，性别，年龄)，如不输入==一直提示输入==</li>
<li>最后根据用户的信息输出相对应的内容</li>
</ol>
<p><strong>思路：</strong></p>
<ol>
<li><p>==交互式==定义多个变量来保存用户信息  姓名、性别、年龄</p>
</li>
<li><p>如果不输一直提示输入</p>
<ul>
<li>==循环==直到输入字符串不为空  while  判断输入字符串是否为空</li>
<li>每个信息都必须不能为空，该功能可以定义为一个函数，方便下面脚本调用</li>
</ul>
</li>
<li><p>根据用户输入信息做出匹配判断</p>
</li>
</ol>
<p><strong>代码实现：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#该函数实现用户如果不输入内容则一直循环直到用户输入为止，并且将用户输入的内容打印出来</span></span><br><span class="line">input_fun()</span><br><span class="line">&#123;</span><br><span class="line">  input_var=<span class="string">&quot;&quot;</span></span><br><span class="line">  output_var=<span class="variable">$1</span></span><br><span class="line">  <span class="keyword">while</span> [ -<span class="type">z</span> <span class="variable">$input_var</span> ]</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	read <span class="literal">-p</span> <span class="string">&quot;<span class="variable">$output_var</span>&quot;</span> input_var</span><br><span class="line">	done</span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$input_var</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">input_fun 请输入你的姓名:</span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">fun()</span><br><span class="line">&#123;</span><br><span class="line">	read <span class="literal">-p</span> <span class="string">&quot;<span class="variable">$1</span>&quot;</span> var</span><br><span class="line">	<span class="keyword">if</span> [ -<span class="type">z</span> <span class="variable">$var</span> ];then</span><br><span class="line">		fun <span class="variable">$1</span></span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="variable">$var</span></span><br><span class="line">	fi</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#调用函数并且获取用户的姓名、性别、年龄分别赋值给name、sex、age变量</span></span><br><span class="line">name=<span class="variable">$</span>(input_fun 请输入你的姓名:)</span><br><span class="line">sex=<span class="variable">$</span>(input_fun 请输入你的性别:)</span><br><span class="line">age=<span class="variable">$</span>(input_fun 请输入你的年龄:)</span><br><span class="line"></span><br><span class="line"><span class="comment">#根据用户输入的性别进行匹配判断</span></span><br><span class="line">case <span class="variable">$sex</span> <span class="keyword">in</span></span><br><span class="line">			<span class="built_in">man</span>)</span><br><span class="line">			<span class="keyword">if</span> [ <span class="variable">$age</span> -<span class="type">gt</span> <span class="number">18</span> -<span class="type">a</span> <span class="variable">$age</span> -<span class="type">le</span> <span class="number">35</span> ];then</span><br><span class="line">				<span class="built_in">echo</span> <span class="string">&quot;中年大叔你油腻了吗？加油&quot;</span></span><br><span class="line">			elif [ <span class="variable">$age</span> -<span class="type">gt</span> <span class="number">35</span> ];then</span><br><span class="line">				<span class="built_in">echo</span> <span class="string">&quot;保温杯里泡枸杞&quot;</span></span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">				<span class="built_in">echo</span> <span class="string">&quot;年轻有为。。。&quot;</span></span><br><span class="line">			fi</span><br><span class="line">			;;</span><br><span class="line">			woman)</span><br><span class="line">			xxx</span><br><span class="line">			;;</span><br><span class="line">			*)</span><br><span class="line">			xxx</span><br><span class="line">			;;</span><br><span class="line">esac</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>扩展延伸：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">描述以下代码含义：	</span><br><span class="line">:()</span><br><span class="line">&#123;</span><br><span class="line">   :|:&amp;</span><br><span class="line">&#125;</span><br><span class="line">:</span><br></pre></td></tr></table></figure>
<p>#三、综合案例</p>
<h2 id="1-任务背景"><a href="#1-任务背景" class="headerlink" title="1. 任务背景"></a>1. 任务背景</h2><p>现有的跳板机虽然实现了统一入口来访问生产服务器，yunwei用户权限太大可以操作跳板机上的所有目录文件，存在数据被误删的安全隐患，所以希望你做一些安全策略来保证跳板机的正常使用。</p>
<h2 id="2-具体要求"><a href="#2-具体要求" class="headerlink" title="2. 具体要求"></a>2. 具体要求</h2><ol>
<li>只允许yunwei用户通过跳板机远程连接后台的应用服务器做一些维护操作</li>
<li>公司运维人员远程通过yunwei用户连接跳板机时，跳出以下菜单供选择：</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">欢迎使用Jumper<span class="literal">-server</span>，请选择你要操作的主机：</span><br><span class="line"><span class="number">1</span>. DB1<span class="literal">-Master</span></span><br><span class="line"><span class="number">2</span>. DB2<span class="literal">-Slave</span></span><br><span class="line"><span class="number">3</span>. Web1</span><br><span class="line"><span class="number">4</span>. Web2</span><br><span class="line"><span class="built_in">h</span>. help</span><br><span class="line">q. <span class="keyword">exit</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>当用户选择相应主机后，直接<strong>免密码登录</strong>成功</li>
<li>如果用户不输入一直提示用户输入，直到用户选择退出</li>
</ol>
<h2 id="3-综合分析"><a href="#3-综合分析" class="headerlink" title="3. 综合分析"></a>3. 综合分析</h2><ol>
<li>将脚本放到yunwei用户家目录里的.bashrc文件里（/shell05/jumper-server.sh）</li>
<li>将菜单定义为一个函数[打印菜单]，方便后面调用</li>
<li>用case语句来实现用户的选择【交互式定义变量】</li>
<li>当用户选择了某一台服务器后，进一步询问用户需要做的事情  case…esac  交互式定义变量</li>
<li>使用循环来实现用户不选择一直让其选择</li>
<li>限制用户退出后直接关闭终端  exit </li>
</ol>
<h2 id="4-落地实现"><a href="#4-落地实现" class="headerlink" title="4. 落地实现"></a>4. 落地实现</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># jumper-server</span></span><br><span class="line"><span class="comment"># 定义菜单打印功能的函数</span></span><br><span class="line">menu()</span><br><span class="line">&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line">欢迎使用Jumper<span class="literal">-server</span>，请选择你要操作的主机：</span><br><span class="line"><span class="number">1</span>. DB1<span class="literal">-Master</span></span><br><span class="line"><span class="number">2</span>. DB2<span class="literal">-Slave</span></span><br><span class="line"><span class="number">3</span>. Web1</span><br><span class="line"><span class="number">4</span>. Web2</span><br><span class="line"><span class="built_in">h</span>. help</span><br><span class="line">q. <span class="keyword">exit</span></span><br><span class="line">	EOF</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 屏蔽以下信号</span></span><br><span class="line"><span class="keyword">trap</span> <span class="string">&#x27;&#x27;</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">19</span></span><br><span class="line"><span class="comment"># 调用函数来打印菜单</span></span><br><span class="line">menu</span><br><span class="line"><span class="comment">#循环等待用户选择</span></span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"><span class="comment"># 菜单选择，case...esac语句</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请选择你要访问的主机:&quot;</span> host</span><br><span class="line">case <span class="variable">$host</span> <span class="keyword">in</span></span><br><span class="line">	<span class="number">1</span>)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="number">2</span>)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.2</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="number">3</span>)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.3</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="built_in">h</span>)</span><br><span class="line">	<span class="built_in">clear</span>;menu</span><br><span class="line">	;;</span><br><span class="line">	q)</span><br><span class="line">	<span class="keyword">exit</span></span><br><span class="line">	;;</span><br><span class="line">esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">将脚本放到yunwei用户家目录里的.bashrc里执行：</span><br><span class="line">bash ~/jumper<span class="literal">-server</span>.sh</span><br><span class="line"><span class="keyword">exit</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>进一步完善需求</strong></p>
<p>为了进一步增强跳板机的安全性，工作人员通过跳板机访问生产环境，但是不能在跳板机上停留。</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#公钥推送成功</span></span><br><span class="line"><span class="keyword">trap</span> <span class="string">&#x27;&#x27;</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">19</span></span><br><span class="line"><span class="comment">#打印菜单用户选择</span></span><br><span class="line">menu()&#123;</span><br><span class="line"><span class="built_in">cat</span> &lt;&lt;<span class="literal">-EOF</span></span><br><span class="line">欢迎使用Jumper<span class="literal">-server</span>，请选择你要操作的主机：</span><br><span class="line"><span class="number">1</span>. DB1<span class="literal">-Master</span></span><br><span class="line"><span class="number">2</span>. DB2<span class="literal">-Slave</span></span><br><span class="line"><span class="number">3</span>. Web1</span><br><span class="line"><span class="number">4</span>. Web2</span><br><span class="line"><span class="built_in">h</span>. help</span><br><span class="line">q. <span class="keyword">exit</span></span><br><span class="line">EOF</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#调用函数来打印菜单</span></span><br><span class="line">menu</span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你要选择的主机[h for help]：&quot;</span> host</span><br><span class="line"></span><br><span class="line"><span class="comment">#通过case语句来匹配用户所输入的主机</span></span><br><span class="line">case <span class="variable">$host</span> <span class="keyword">in</span></span><br><span class="line">	<span class="number">1</span>|DB1)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="number">2</span>|DB2)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.2</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="number">3</span>|web1)</span><br><span class="line">	ssh root@<span class="number">10.1</span>.<span class="number">1.250</span></span><br><span class="line">	;;</span><br><span class="line">	<span class="built_in">h</span>|help)</span><br><span class="line">	<span class="built_in">clear</span>;menu</span><br><span class="line">	;;</span><br><span class="line">	q|quit)</span><br><span class="line">	<span class="keyword">exit</span></span><br><span class="line">	;;</span><br><span class="line">esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">自己完善功能：</span><br><span class="line"><span class="number">1</span>. 用户选择主机后，需要事先推送公钥；如何判断公钥是否已推</span><br><span class="line"><span class="number">2</span>. 比如选择web1时，再次提示需要做的操作，比如：</span><br><span class="line">clean log</span><br><span class="line">重启服务</span><br><span class="line"><span class="built_in">kill</span>某个进程</span><br></pre></td></tr></table></figure>
<p><strong>回顾信号：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) SIGHUP 			重新加载配置    </span><br><span class="line"><span class="number">2</span>) SIGINT			键盘中断^C</span><br><span class="line"><span class="number">3</span>) SIGQUIT      	键盘退出</span><br><span class="line"><span class="number">9</span>) SIGKILL		 	强制终止</span><br><span class="line"><span class="number">15</span>) SIGTERM	    	终止（正常结束），缺省信号</span><br><span class="line"><span class="number">18</span>) SIGCONT	   	继续</span><br><span class="line"><span class="number">19</span>) SIGSTOP	   	停止</span><br><span class="line"><span class="number">20</span>) SIGTSTP     	暂停^Z</span><br></pre></td></tr></table></figure>
<h1 id="四、正则表达式"><a href="#四、正则表达式" class="headerlink" title="四、正则表达式"></a>四、正则表达式</h1><p>##1. 正则表达式是什么？</p>
<p><strong>正则表达式</strong>（Regular Expression、regex或regexp，缩写为RE），也译为正规表示法、常规表示法，是一种字符模式，用于在查找过程中==匹配指定的字符==。</p>
<p>许多程序设计语言都支持利用正则表达式进行<strong>字符串操作</strong>。例如，在Perl中就内建了一个功能强大的正则表达式引擎。</p>
<p>正则表达式这个概念最初是由Unix中的工具软件（例如sed和grep）普及开的。</p>
<p>支持正则表达式的程序如：locate |find| vim| grep| sed |awk</p>
<h2 id="2-正则能干什么？"><a href="#2-正则能干什么？" class="headerlink" title="2. 正则能干什么？"></a>2. 正则能干什么？</h2><ol>
<li>匹配邮箱、匹配身份证号码、手机号、银行卡号等</li>
<li>匹配某些特定字符串，做特定处理等等</li>
</ol>
<h2 id="3-正则当中名词解释"><a href="#3-正则当中名词解释" class="headerlink" title="3. 正则当中名词解释"></a>3. 正则当中名词解释</h2><ul>
<li><p><strong>元字符</strong></p>
<p>指那些在正则表达式中具有<strong>特殊意义的==专用字符==</strong>,如:点(.) 星(*) 问号(?)等</p>
</li>
<li><p><strong>前导字符</strong></p>
<p>位于<strong>元字符</strong>前面的字符.    ab*<em>==c==**</em>   aoo**==o==.**</p>
</li>
</ul>
<p>##4. 第一类正则表达式</p>
<h3 id="㈠-正则中普通常用的元字符"><a href="#㈠-正则中普通常用的元字符" class="headerlink" title="㈠ 正则中普通常用的元字符"></a>㈠ 正则中普通常用的元字符</h3><table>
<thead>
<tr>
<th>元字符</th>
<th>功能</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>.</td>
<td>匹配除了换行符以外的==任意单个==字符</td>
<td></td>
</tr>
<tr>
<td>*</td>
<td>==前导字符==出现==0==次或==连续多次==</td>
<td></td>
</tr>
<tr>
<td>.*</td>
<td>任意长度字符</td>
<td>ab.*</td>
</tr>
<tr>
<td>^</td>
<td>行首(以…开头)</td>
<td>^root</td>
</tr>
<tr>
<td>$</td>
<td>行尾(以…结尾)</td>
<td>bash$</td>
</tr>
<tr>
<td>^$</td>
<td>空行</td>
<td></td>
</tr>
<tr>
<td>[]</td>
<td>匹配括号里任意单个字符或一组单个字符</td>
<td>[abc]</td>
</tr>
<tr>
<td>[^]</td>
<td>匹配不包含括号里任一单个字符或一组单个字符</td>
<td>[^abc]</td>
</tr>
<tr>
<td>^[]</td>
<td>匹配以括号里任意单个字符或一组单个字符开头</td>
<td>^[abc]</td>
</tr>
<tr>
<td>^[^]</td>
<td>匹配不以括号里任意单个字符或一组单个字符开头</td>
<td>^[^abc]</td>
</tr>
</tbody></table>
<ul>
<li>示例文本</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># cat 1.txt</span></span><br><span class="line">ggle</span><br><span class="line">gogle</span><br><span class="line">google</span><br><span class="line">gooogle</span><br><span class="line">goooooogle</span><br><span class="line">gooooooogle</span><br><span class="line">taobao.com</span><br><span class="line">taotaobaobao.com</span><br><span class="line"></span><br><span class="line">jingdong.com</span><br><span class="line">dingdingdongdong.com</span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">Adfjd8789JHfdsdf/</span><br><span class="line">a87fdjfkdLKJK</span><br><span class="line"><span class="number">7</span>kdjfd989KJK;</span><br><span class="line">bSKJjkksdjf878.</span><br><span class="line">cidufKJHJ6576,</span><br><span class="line"></span><br><span class="line">hello world</span><br><span class="line">helloworld yourself</span><br></pre></td></tr></table></figure>
<ul>
<li>举例说明</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="㈡-正则中其他常用元字符"><a href="#㈡-正则中其他常用元字符" class="headerlink" title="㈡ 正则中其他常用元字符"></a>㈡ 正则中其他常用元字符</h3><table>
<thead>
<tr>
<th>元字符</th>
<th>功能</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>\&lt;</td>
<td>取单词的头</td>
<td></td>
</tr>
<tr>
<td>\&gt;</td>
<td>取单词的尾</td>
<td></td>
</tr>
<tr>
<td>\&lt;  \&gt;</td>
<td>精确匹配</td>
<td></td>
</tr>
<tr>
<td>\{n\}</td>
<td>匹配前导字符==连续出现n次==</td>
<td></td>
</tr>
<tr>
<td>\{n,\}</td>
<td>匹配前导字符==至少出现n次==</td>
<td></td>
</tr>
<tr>
<td>\{n,m\}</td>
<td>匹配前导字符出现==n次与m次之间==</td>
<td></td>
</tr>
<tr>
<td>\(   \)</td>
<td>保存被匹配的字符</td>
<td></td>
</tr>
<tr>
<td>\d</td>
<td>匹配数字（<strong>grep -P</strong>）</td>
<td>[0-9]</td>
</tr>
<tr>
<td>\w</td>
<td>匹配字母数字下划线（<strong>grep -P</strong>）</td>
<td>[a-zA-Z0-9_]</td>
</tr>
<tr>
<td>\s</td>
<td>匹配空格、制表符、换页符（<strong>grep -P</strong>）</td>
<td>[\t\r\n]</td>
</tr>
</tbody></table>
<p><strong>举例说明：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">需求：将<span class="number">10.1</span>.<span class="number">1.1</span>替换成<span class="number">10.1</span>.<span class="number">1.254</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>）vim编辑器支持正则表达式</span><br><span class="line"><span class="comment"># vim 1.txt</span></span><br><span class="line">:%s<span class="comment">#\(10.1.1\).1#\1.254#g </span></span><br><span class="line">:%s/\(<span class="number">10.1</span>.<span class="number">1</span>\).<span class="number">1</span>/\<span class="number">1.254</span>/g </span><br><span class="line"></span><br><span class="line"><span class="number">2</span>）sed支持正则表达式【后面学】</span><br><span class="line"><span class="comment"># sed -n &#x27;s#\(10.1.1\).1#\1.254#p&#x27; 1.txt</span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.254</span></span><br><span class="line"></span><br><span class="line">说明：</span><br><span class="line">找出含有<span class="number">10.1</span>.<span class="number">1</span>的行，同时保留<span class="number">10.1</span>.<span class="number">1</span>并标记为标签<span class="number">1</span>，之后可以使用\<span class="number">1</span>来引用它。</span><br><span class="line">最多可以定义<span class="number">9</span>个标签，从左边开始编号，最左边的是第一个。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">需求：将helloworld yourself 换成hellolilei myself</span><br><span class="line"></span><br><span class="line"><span class="comment"># vim 1.txt</span></span><br><span class="line">:%s<span class="comment">#\(hello\)world your\(self\)#\1lilei my\2#g</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># sed -n &#x27;s/\(hello\)world your\(self\)/\1lilei my\2/p&#x27; 1.txt </span></span><br><span class="line">hellolilei myself</span><br><span class="line"></span><br><span class="line"><span class="comment"># sed -n &#x27;s/helloworld yourself/hellolilei myself/p&#x27; 1.txt </span></span><br><span class="line">hellolilei myself</span><br><span class="line"><span class="comment"># sed -n &#x27;s/\(hello\)world your\(self\)/\1lilei my\2/p&#x27; 1.txt </span></span><br><span class="line">hellolilei myself</span><br><span class="line"></span><br><span class="line">Perl内置正则：</span><br><span class="line">\d      匹配数字  [<span class="number">0</span>-<span class="number">9</span>]</span><br><span class="line">\w      匹配字母数字下划线[<span class="type">a</span>-<span class="type">zA</span>-<span class="type">Z0</span>-<span class="number">9</span><span class="type">_</span>]</span><br><span class="line">\s      匹配空格、制表符、换页符[\<span class="type">t</span>\<span class="type">r</span>\<span class="type">n</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># grep -P &#x27;\d&#x27; 1.txt</span></span><br><span class="line"><span class="comment"># grep -P &#x27;\w&#x27; 2.txt</span></span><br><span class="line"><span class="comment"># grep -P &#x27;\s&#x27; 3.txt</span></span><br></pre></td></tr></table></figure>
<h3 id="㈢-扩展类正则常用元字符"><a href="#㈢-扩展类正则常用元字符" class="headerlink" title="㈢ 扩展类正则常用元字符"></a>㈢ 扩展类正则常用元字符</h3><p><strong>==丑话说在前面：==</strong></p>
<p>我说我比较特殊，你要相信！否则我错给你看:smirk:</p>
<ul>
<li><p>grep你要用我，必须加 <strong>==-E==</strong>  或者  让你兄弟<code>egrep</code>来找我</p>
</li>
<li><p>sed你要用我，必须加 <strong>==-r==</strong></p>
</li>
</ul>
<table>
<thead>
<tr>
<th>扩展元字符</th>
<th>功能</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>匹配一个或多个前导字符</td>
<td>bo+ 匹配boo、 bo</td>
</tr>
<tr>
<td>?</td>
<td>匹配零个或一个前导字符</td>
<td>bo? 匹配b、 bo</td>
</tr>
<tr>
<td>|</td>
<td>或</td>
<td>匹配a或b</td>
</tr>
<tr>
<td>()</td>
<td>组字符（看成整体）</td>
<td>(my|your)self：表示匹配myself或匹配yourself</td>
</tr>
<tr>
<td>{n}</td>
<td>前导字符重复n次</td>
<td></td>
</tr>
<tr>
<td>{n,}</td>
<td>前导字符重复至少n次</td>
<td></td>
</tr>
<tr>
<td>{n,m}</td>
<td>前导字符重复n到m次</td>
<td></td>
</tr>
</tbody></table>
<p><strong>举例说明：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># grep &quot;root|ftp|adm&quot; /etc/passwd</span></span><br><span class="line"><span class="comment"># egrep &quot;root|ftp|adm&quot; /etc/passwd</span></span><br><span class="line"><span class="comment"># grep -E &quot;root|ftp|adm&quot; /etc/passwd</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># grep -E &#x27;o+gle&#x27; test.txt </span></span><br><span class="line"><span class="comment"># grep -E &#x27;o?gle&#x27; test.txt </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># egrep &#x27;go&#123;2,&#125;&#x27; 1.txt</span></span><br><span class="line"><span class="comment"># egrep &#x27;(my|your)self&#x27; 1.txt</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">使用正则过滤出文件中的IP地址：</span><br><span class="line"><span class="comment"># grep &#x27;[0-9]\&#123;2\&#125;\.[0-9]\&#123;1\&#125;\.[0-9]\&#123;1\&#125;\.[0-9]\&#123;1\&#125;&#x27; 1.txt </span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="comment"># grep &#x27;[0-9]&#123;2&#125;\.[0-9]&#123;1&#125;\.[0-9]&#123;1&#125;\.[0-9]&#123;1&#125;&#x27; 1.txt </span></span><br><span class="line"><span class="comment"># grep -E &#x27;[0-9]&#123;2&#125;\.[0-9]&#123;1&#125;\.[0-9]&#123;1&#125;\.[0-9]&#123;1&#125;&#x27; 1.txt </span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="comment"># grep -E &#x27;[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;&#x27; 1.txt </span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="comment"># grep -E &#x27;([0-9]&#123;1,3&#125;\.)&#123;3&#125;[0-9]&#123;1,3&#125;&#x27; 1.txt </span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>##5. 第二类正则</p>
<table>
<thead>
<tr>
<th>表达式</th>
<th>功能</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>[:alnum:]</td>
<td>字母与数字字符</td>
<td>[[:alnum:]]+</td>
</tr>
<tr>
<td>[:alpha:]</td>
<td>字母字符(包括大小写字母)</td>
<td>[[:alpha:]]{4}</td>
</tr>
<tr>
<td>[:blank:]</td>
<td>空格与制表符</td>
<td>[[:blank:]]*</td>
</tr>
<tr>
<td>[:digit:]</td>
<td>数字</td>
<td>[[:digit:]]?</td>
</tr>
<tr>
<td>[:lower:]</td>
<td>小写字母</td>
<td>[[:lower:]]{4,}</td>
</tr>
<tr>
<td>[:upper:]</td>
<td>大写字母</td>
<td>[[:upper:]]+</td>
</tr>
<tr>
<td>[:punct:]</td>
<td>标点符号</td>
<td>[[:punct:]]</td>
</tr>
<tr>
<td>[:space:]</td>
<td>包括换行符，回车等在内的所有空白</td>
<td>[[:space:]]+</td>
</tr>
</tbody></table>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell05</span>]<span class="comment"># grep -E &#x27;^[[:digit:]]+&#x27; 1.txt</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell05</span>]<span class="comment"># grep -E &#x27;^[^[:digit:]]+&#x27; 1.txt</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell05</span>]<span class="comment"># grep -E &#x27;[[:lower:]]&#123;4,&#125;&#x27; 1.txt</span></span><br></pre></td></tr></table></figure>
<h2 id="6-正则表达式总结"><a href="#6-正则表达式总结" class="headerlink" title="6. 正则表达式总结"></a>6. 正则表达式总结</h2><p><strong>把握一个原则，让你轻松搞定可恶的正则符号：</strong></p>
<ol>
<li>我要找什么？<ul>
<li>找数字                            [0-9]</li>
<li>找字母                                    [a-zA-Z]</li>
<li>找标点符号                            [[:punct:]]</li>
</ul>
</li>
<li>我要如何找？看心情找<ul>
<li>以什么为首                           ^key</li>
<li>以什么结尾                           key$</li>
<li>包含什么或不包含什么        [abc]  ^[abc]   [^abc]   ^[^abc]</li>
</ul>
</li>
<li>我要找多少呀？<ul>
<li>找前导字符出现0次或连续多次             ab==*==</li>
<li>找任意单个(一次)字符                             ab==.==</li>
<li>找任意字符                                               ab==.*==</li>
<li>找前导字符连续出现几次                        {n}  {n,m}   {n,}</li>
<li>找前导字符出现1次或多次                      go==+==</li>
<li> 找前到字符出现0次或1次                       go==?==                  </li>
</ul>
</li>
</ol>
<h1 id="五、正则元字符一栏表"><a href="#五、正则元字符一栏表" class="headerlink" title="五、正则元字符一栏表"></a>五、正则元字符一栏表</h1><p><strong>元字符</strong>：在正则中，具有特殊意义的专用字符，如: 星号(*)、加号(+)等</p>
<p><strong>前导字符</strong>：元字符前面的字符叫前导字符</p>
<table>
<thead>
<tr>
<th>元字符</th>
<th>功能</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>*</td>
<td>前导字符出现0次或者连续多次</td>
<td>ab*  abbbb</td>
</tr>
<tr>
<td>.</td>
<td>除了换行符以外，任意单个字符</td>
<td>ab.   ab8 abu</td>
</tr>
<tr>
<td>.*</td>
<td>任意长度的字符</td>
<td>ab.*  adfdfdf</td>
</tr>
<tr>
<td>[]</td>
<td>括号里的任意单个字符或一组单个字符</td>
<td>[abc][0-9][a-z]</td>
</tr>
<tr>
<td>[^]</td>
<td>不匹配括号里的任意单个字符或一组单个字符</td>
<td>[^abc]</td>
</tr>
<tr>
<td>^[]</td>
<td>匹配以括号里的任意单个字符开头</td>
<td>^[abc]</td>
</tr>
<tr>
<td>^[^]</td>
<td>不匹配以括号里的任意单个字符开头</td>
<td></td>
</tr>
<tr>
<td>^</td>
<td>行的开头</td>
<td>^root</td>
</tr>
<tr>
<td>$</td>
<td>行的结尾</td>
<td>bash$</td>
</tr>
<tr>
<td>^$</td>
<td>空行</td>
<td></td>
</tr>
<tr>
<td>\{n\}和{n}</td>
<td>前导字符连续出现n次</td>
<td>[0-9]\{3\}</td>
</tr>
<tr>
<td>\{n,\}和{n,}</td>
<td>前导字符至少出现n次</td>
<td>[a-z]{4,}</td>
</tr>
<tr>
<td>\{n,m\}和{n,m}</td>
<td>前导字符连续出现n-m次</td>
<td>go{2,4}</td>
</tr>
<tr>
<td>\&lt;\&gt;</td>
<td>精确匹配单词</td>
<td>\&lt;hello\&gt;</td>
</tr>
<tr>
<td>\(\)</td>
<td>保留匹配到的字符</td>
<td>\(hello\)</td>
</tr>
<tr>
<td>+</td>
<td>前导字符出现1次或者多次</td>
<td>[0-9]+</td>
</tr>
<tr>
<td>?</td>
<td>前导字符出现0次或者1次</td>
<td>go?</td>
</tr>
<tr>
<td>|</td>
<td>或</td>
<td>^root|^ftp</td>
</tr>
<tr>
<td>()</td>
<td>组字符</td>
<td>(hello|world)123</td>
</tr>
<tr>
<td>\d</td>
<td>perl内置正则</td>
<td>grep -P  \d+</td>
</tr>
<tr>
<td>\w</td>
<td>匹配字母数字下划线</td>
<td></td>
</tr>
</tbody></table>
<h1 id="六、正则练习作业"><a href="#六、正则练习作业" class="headerlink" title="六、正则练习作业"></a>六、正则练习作业</h1><h2 id="1-文件准备"><a href="#1-文件准备" class="headerlink" title="1. 文件准备"></a>1. 文件准备</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># vim test.txt </span></span><br><span class="line">Aieur45869Root0000</span><br><span class="line"><span class="number">9</span>h847RkjfkIIIhello</span><br><span class="line">rootHllow88000dfjj</span><br><span class="line"><span class="number">8</span>ikuioerhfhupliooking</span><br><span class="line">hello world</span><br><span class="line"><span class="number">192.168</span>.<span class="number">0.254</span></span><br><span class="line">welcome to uplooking.</span><br><span class="line">abcderfkdjfkdtest</span><br><span class="line">rlllA899kdfkdfj</span><br><span class="line">iiiA848890ldkfjdkfj</span><br><span class="line">abc</span><br><span class="line"><span class="number">12345678908374</span></span><br><span class="line"><span class="number">123456</span>@qq.com</span><br><span class="line"><span class="number">123456</span>@<span class="number">163</span>.com</span><br><span class="line">abcdefg@itcast.com23ed</span><br></pre></td></tr></table></figure>
<h2 id="2-具体要求-1"><a href="#2-具体要求-1" class="headerlink" title="2. 具体要求"></a>2. 具体要求</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、查找不以大写字母开头的行（三种写法）。</span><br><span class="line">grep <span class="string">&#x27;^[^A-Z]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line">grep <span class="literal">-v</span> <span class="string">&#x27;^[A-Z]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line">grep <span class="string">&#x27;^[^[:upper:]]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">2</span>、查找有数字的行（两种写法）</span><br><span class="line">grep <span class="string">&#x27;[0-9]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line">grep <span class="literal">-P</span> <span class="string">&#x27;\d&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">3</span>、查找一个数字和一个字母连起来的</span><br><span class="line">grep <span class="literal">-E</span> <span class="string">&#x27;[0-9][a-zA-Z]|[a-zA-Z][0-9]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">4</span>、查找不以<span class="built_in">r</span>开头的行</span><br><span class="line">grep <span class="literal">-v</span> <span class="string">&#x27;^r&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line">grep <span class="string">&#x27;^[^r]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">5</span>、查找以数字开头的</span><br><span class="line">grep <span class="string">&#x27;^[0-9]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">6</span>、查找以大写字母开头的</span><br><span class="line">grep <span class="string">&#x27;^[A-Z]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">7</span>、查找以小写字母开头的</span><br><span class="line">grep <span class="string">&#x27;^[a-z]&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">8</span>、查找以点结束的</span><br><span class="line">grep <span class="string">&#x27;\.$&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">9</span>、去掉空行</span><br><span class="line">grep <span class="literal">-v</span> <span class="string">&#x27;^$&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">10</span>、查找完全匹配abc的行</span><br><span class="line">grep <span class="string">&#x27;\&lt;abc\&gt;&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">11</span>、查找A后有三个数字的行</span><br><span class="line">grep <span class="literal">-E</span> <span class="string">&#x27;A[0-9]&#123;3&#125;&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line">grep  <span class="string">&#x27;A[0-9]\&#123;3\&#125;&#x27;</span> <span class="number">2</span>.txt</span><br><span class="line"><span class="number">12</span>、统计root在/etc/passwd里出现了几次</span><br><span class="line">grep <span class="literal">-o</span> <span class="string">&#x27;root&#x27;</span> <span class="number">1</span>.txt |wc <span class="literal">-l</span></span><br><span class="line"></span><br><span class="line"><span class="number">13</span>、用正则表达式找出自己的IP地址、广播地址、子网掩码</span><br><span class="line">ifconfig eth0|grep Bcast|grep <span class="literal">-o</span> <span class="string">&#x27;[0-9]\&#123;1,3\&#125;\.[0-9]\&#123;1,3\&#125;\.[0-9]\&#123;1,3\&#125;\.[0-9]\&#123;1,3\&#125;&#x27;</span></span><br><span class="line">ifconfig eth0|grep Bcast| grep <span class="literal">-E</span> <span class="literal">-o</span> <span class="string">&#x27;([0-9]&#123;1,3&#125;.)&#123;3&#125;[0-9]&#123;1,3&#125;&#x27;</span></span><br><span class="line">ifconfig eth0|grep Bcast| grep <span class="literal">-P</span> <span class="literal">-o</span> <span class="string">&#x27;\d&#123;1,3&#125;.\d&#123;1,3&#125;.\d&#123;1,3&#125;.\d&#123;1,3&#125;&#x27;</span></span><br><span class="line">ifconfig eth0|grep Bcast| grep <span class="literal">-P</span> <span class="literal">-o</span> <span class="string">&#x27;(\d&#123;1,3&#125;.)&#123;3&#125;\d&#123;1,3&#125;&#x27;</span></span><br><span class="line">ifconfig eth0|grep Bcast| grep <span class="literal">-P</span> <span class="literal">-o</span> <span class="string">&#x27;(\d+.)&#123;3&#125;\d+&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># egrep --color &#x27;[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;\.[0-9]&#123;1,3&#125;&#x27; /etc/sysconfig/network-scripts/ifcfg-eth0</span></span><br><span class="line">IPADDR=<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">NETMASK=<span class="number">255.255</span>.<span class="number">255.0</span></span><br><span class="line">GATEWAY=<span class="number">10.1</span>.<span class="number">1.254</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># egrep --color &#x27;[[:digit:]]&#123;1,3&#125;\.[[:digit:]]&#123;1,3&#125;\.[[:digit:]]&#123;1,3&#125;\.[[:digit:]]&#123;1,3&#125;&#x27; /etc/sysconfig/network-scripts/ifcfg-eth0 </span></span><br><span class="line">IPADDR=<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line">NETMASK=<span class="number">255.255</span>.<span class="number">255.0</span></span><br><span class="line">GATEWAY=<span class="number">10.1</span>.<span class="number">1.254</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">14</span>、找出文件中的ip地址并且打印替换成<span class="number">172.16</span>.<span class="number">2.254</span></span><br><span class="line">grep <span class="literal">-o</span> <span class="literal">-E</span> <span class="string">&#x27;([0-9]&#123;1,3&#125;\.)&#123;3&#125;[0-9]&#123;1,3&#125;&#x27;</span> <span class="number">1</span>.txt |sed <span class="literal">-n</span> <span class="string">&#x27;s/192.168.0.\(254\)/172.16.2.\1/p&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="number">15</span>、找出文件中的ip地址</span><br><span class="line">grep <span class="literal">-o</span> <span class="literal">-E</span> <span class="string">&#x27;([0-9]&#123;1,3&#125;\.)&#123;3&#125;[0-9]&#123;1,3&#125;&#x27;</span> <span class="number">1</span>.txt</span><br><span class="line"></span><br><span class="line"><span class="number">16</span>、找出全部是数字的行</span><br><span class="line">grep <span class="literal">-E</span> <span class="string">&#x27;^[0-9]+$&#x27;</span> test</span><br><span class="line"><span class="number">17</span>、找出邮箱地址</span><br><span class="line">grep <span class="literal">-E</span> <span class="string">&#x27;^[0-9]+@[a-z0-9]+\.[a-z]+$&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">grep -<span class="literal">-help</span>:</span><br><span class="line">匹配模式选择：</span><br><span class="line">Regexp selection and interpretation:</span><br><span class="line">  <span class="literal">-E</span>, -<span class="literal">-extended</span><span class="literal">-regexp</span>     扩展正则</span><br><span class="line">  <span class="literal">-G</span>, -<span class="literal">-basic</span><span class="literal">-regexp</span>        基本正则</span><br><span class="line">  <span class="literal">-P</span>, -<span class="literal">-perl</span><span class="literal">-regexp</span>         调用perl的正则</span><br><span class="line">  <span class="literal">-e</span>, -<span class="literal">-regexp</span>=PATTERN      use PATTERN <span class="keyword">for</span> matching</span><br><span class="line">  <span class="operator">-f</span>, -<span class="operator">-file</span>=FILE           obtain PATTERN from FILE</span><br><span class="line">  <span class="literal">-i</span>, -<span class="literal">-ignore</span><span class="literal">-case</span>         忽略大小写</span><br><span class="line">  <span class="literal">-w</span>, -<span class="literal">-word</span><span class="literal">-regexp</span>         匹配整个单词</span><br><span class="line">  </span><br></pre></td></tr></table></figure>
<p>#七、课后作业</p>
<h2 id="脚本搭建web服务"><a href="#脚本搭建web服务" class="headerlink" title="脚本搭建web服务"></a>脚本搭建web服务</h2><p><strong>要求如下</strong>：</p>
<ol>
<li>用户输入web服务器的IP、域名以及数据根目录</li>
<li>如果用户不输入则一直提示输入，直到输入为止</li>
<li>当访问<span class="exturl" data-url="aHR0cDovL3d3dy50ZXN0LmNj5pe25Y+v5Lul6K6/6Zeu5Yiw5pWw5o2u5qC555uu5b2V6YeM55qE6aaW6aG15paH5Lu24oCcdGhpcy8=">www.test.cc时可以访问到数据根目录里的首页文件“this<i class="fa fa-external-link-alt"></i></span> is test page” </li>
</ol>
<p><strong>参考脚本：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">参考：</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">conf=/etc/httpd/conf/httpd.conf</span><br><span class="line">input_fun()</span><br><span class="line">&#123;</span><br><span class="line">  input_var=<span class="string">&quot;&quot;</span></span><br><span class="line">  output_var=<span class="variable">$1</span></span><br><span class="line">  <span class="keyword">while</span> [ -<span class="type">z</span> <span class="variable">$input_var</span> ]</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	read <span class="literal">-p</span> <span class="string">&quot;<span class="variable">$output_var</span>&quot;</span> input_var</span><br><span class="line">	done</span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$input_var</span></span><br><span class="line">&#125;</span><br><span class="line">ipaddr=<span class="variable">$</span>(input_fun <span class="string">&quot;Input Host ip[192.168.0.1]:&quot;</span>)</span><br><span class="line">web_host_name=<span class="variable">$</span>(input_fun <span class="string">&quot;Input VirtualHostName [www.test.cc]:&quot;</span>)</span><br><span class="line">root_dir=<span class="variable">$</span>(input_fun <span class="string">&quot;Input host Documentroot dir:[/var/www/html]:&quot;</span>)</span><br><span class="line"></span><br><span class="line">[ ! -<span class="type">d</span> <span class="variable">$root_dir</span> ] &amp;&amp; mkdir <span class="literal">-p</span> <span class="variable">$root_dir</span></span><br><span class="line">chown apache.apache <span class="variable">$root_dir</span> &amp;&amp; chmod <span class="number">755</span> <span class="variable">$root_dir</span></span><br><span class="line"><span class="built_in">echo</span> this is <span class="variable">$web_host_name</span> &gt; <span class="variable">$root_dir</span>/index.html</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ipaddr</span> <span class="variable">$web_host_name</span>&quot;</span> &gt;&gt; /etc/hosts</span><br><span class="line"></span><br><span class="line">[ -<span class="type">f</span> <span class="variable">$conf</span> ] &amp;&amp; <span class="built_in">cat</span> &gt;&gt; <span class="variable">$conf</span> &lt;&lt;<span class="keyword">end</span></span><br><span class="line">NameVirtualHost <span class="variable">$ipaddr:80</span></span><br><span class="line">&lt;VirtualHost <span class="variable">$ipaddr:80</span>&gt;</span><br><span class="line">	ServerAdmin webmaster<span class="selector-tag">@</span><span class="variable">$web_host_name</span></span><br><span class="line">	DocumentRoot <span class="variable">$root_dir</span></span><br><span class="line">	ServerName <span class="variable">$web_host_name</span></span><br><span class="line">	ErrorLog logs/<span class="variable">$web_host_name</span><span class="literal">-error_log</span></span><br><span class="line">	CustomLog logs/<span class="variable">$web_host_name</span><span class="literal">-access_loh</span> common</span><br><span class="line">&lt;/VirtualHost&gt;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/05/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/05_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF+%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%8F%8A%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B/" rel="prev" title="05_SHELL编程之嵌套循环+随机数及综合案例">
      <i class="fa fa-chevron-left"></i> 05_SHELL编程之嵌套循环+随机数及综合案例
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/07/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/07_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E6%96%87%E6%9C%AC%E5%A4%84%E7%90%86%E5%B7%A5%E5%85%B7sed/" rel="next" title="07_SHELL编程之文本处理工具sed">
      07_SHELL编程之文本处理工具sed <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81case%E8%AF%AD%E5%8F%A5"><span class="nav-number">1.</span> <span class="nav-text">一、case语句</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%AF%AD%E6%B3%95%E7%BB%93%E6%9E%84"><span class="nav-number">1.1.</span> <span class="nav-text">1. 语法结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="nav-number">1.2.</span> <span class="nav-text">2. 应用案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E8%84%9A%E6%9C%AC%E4%BC%A0%E4%B8%8D%E5%90%8C%E5%80%BC%E5%81%9A%E4%B8%8D%E5%90%8C%E4%BA%8B"><span class="nav-number">1.2.1.</span> <span class="nav-text">㈠ 脚本传不同值做不同事</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E6%A0%B9%E6%8D%AE%E7%94%A8%E6%88%B7%E9%9C%80%E6%B1%82%E9%80%89%E6%8B%A9%E5%81%9A%E4%BA%8B"><span class="nav-number">1.2.2.</span> <span class="nav-text">㈡ 根据用户需求选择做事</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81-%E5%87%BD%E6%95%B0"><span class="nav-number">2.</span> <span class="nav-text">二、&#x3D;&#x3D;函数&#x3D;&#x3D;</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E5%87%BD%E6%95%B0%EF%BC%9F"><span class="nav-number">2.1.</span> <span class="nav-text">1. 什么是函数？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%EF%BC%9F"><span class="nav-number">2.2.</span> <span class="nav-text">2. 如何定义函数？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%BD%93%E5%89%8D%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E7%94%A8"><span class="nav-number">2.2.1.</span> <span class="nav-text">㈠ 当前命令行调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%AE%9A%E4%B9%89%E5%88%B0%E7%94%A8%E6%88%B7%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E4%B8%AD"><span class="nav-number">2.2.2.</span> <span class="nav-text">㈡ 定义到用户的环境变量中</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E8%84%9A%E6%9C%AC%E4%B8%AD%E8%B0%83%E7%94%A8"><span class="nav-number">2.2.3.</span> <span class="nav-text">㈢ 脚本中调用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%BB%E5%8A%A1%E8%83%8C%E6%99%AF"><span class="nav-number">2.3.</span> <span class="nav-text">1. 任务背景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%85%B7%E4%BD%93%E8%A6%81%E6%B1%82"><span class="nav-number">2.4.</span> <span class="nav-text">2. 具体要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BB%BC%E5%90%88%E5%88%86%E6%9E%90"><span class="nav-number">2.5.</span> <span class="nav-text">3. 综合分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">2.6.</span> <span class="nav-text">4. 落地实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">3.</span> <span class="nav-text">四、正则表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%AD%A3%E5%88%99%E8%83%BD%E5%B9%B2%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-number">3.1.</span> <span class="nav-text">2. 正则能干什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%AD%A3%E5%88%99%E5%BD%93%E4%B8%AD%E5%90%8D%E8%AF%8D%E8%A7%A3%E9%87%8A"><span class="nav-number">3.2.</span> <span class="nav-text">3. 正则当中名词解释</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E6%AD%A3%E5%88%99%E4%B8%AD%E6%99%AE%E9%80%9A%E5%B8%B8%E7%94%A8%E7%9A%84%E5%85%83%E5%AD%97%E7%AC%A6"><span class="nav-number">3.2.1.</span> <span class="nav-text">㈠ 正则中普通常用的元字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E6%AD%A3%E5%88%99%E4%B8%AD%E5%85%B6%E4%BB%96%E5%B8%B8%E7%94%A8%E5%85%83%E5%AD%97%E7%AC%A6"><span class="nav-number">3.2.2.</span> <span class="nav-text">㈡ 正则中其他常用元字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E6%89%A9%E5%B1%95%E7%B1%BB%E6%AD%A3%E5%88%99%E5%B8%B8%E7%94%A8%E5%85%83%E5%AD%97%E7%AC%A6"><span class="nav-number">3.2.3.</span> <span class="nav-text">㈢ 扩展类正则常用元字符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%80%BB%E7%BB%93"><span class="nav-number">3.3.</span> <span class="nav-text">6. 正则表达式总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E6%AD%A3%E5%88%99%E5%85%83%E5%AD%97%E7%AC%A6%E4%B8%80%E6%A0%8F%E8%A1%A8"><span class="nav-number">4.</span> <span class="nav-text">五、正则元字符一栏表</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E6%AD%A3%E5%88%99%E7%BB%83%E4%B9%A0%E4%BD%9C%E4%B8%9A"><span class="nav-number">5.</span> <span class="nav-text">六、正则练习作业</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%96%87%E4%BB%B6%E5%87%86%E5%A4%87"><span class="nav-number">5.1.</span> <span class="nav-text">1. 文件准备</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%85%B7%E4%BD%93%E8%A6%81%E6%B1%82-1"><span class="nav-number">5.2.</span> <span class="nav-text">2. 具体要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E6%90%AD%E5%BB%BAweb%E6%9C%8D%E5%8A%A1"><span class="nav-number">5.3.</span> <span class="nav-text">脚本搭建web服务</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
